<template>
  <div id="addPicturePage">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '修改用户' : '创建用户' }}
    </h2>
    <!-- 图片信息表单   -->
    <PictureUpload :pictureUrl="userForm.avatar" :on-success="onSuccess"  :category="0" style="width: 120px; margin-bottom: 20px"/>
    <a-form
      name="userForm"
      v-if="authUser"
      layout="vertical"
      :model="userForm"
      @finish="handleSubmit"
    >
      <a-form-item name="name" label="名称">
        <a-input v-model:value="userForm.userName" placeholder="请输入名称" allow-clear />
      </a-form-item>
      <a-form-item name="nickName" label="昵称">
        <a-input v-model:value="userForm.nickName" placeholder="请输入昵称" allow-clear />
      </a-form-item>
      <a-form-item
        label="手机号"
        name="phone"
        :rules="[
            { required: true, message: '请输入手机号!' },
            { min: 11,max:11,  message: '确认手机号长度为11位' },
          ]"
      >
        <a-input v-model:value="userForm.phone" placeholder="请输入手机号" />
      </a-form-item>
      <a-form-item name="sex" label="性别">
        <a-radio-group v-model:value="userForm.sex">
          <a-radio-button :value="'0'">男</a-radio-button>
          <a-radio-button :value="'1'">女</a-radio-button>
        </a-radio-group>
      </a-form-item>
      <a-form-item
        name="password"
        :rules="[
            { required: true, message: '请输入密码!' },
            { min: 8, message: '密码长度不能小于8位' },
          ]"
      >
        <a-input-password v-model:value="userForm.password" placeholder="请输入密码" />
      </a-form-item>
      <a-form-item
        name="checkPassword"
        :rules="[
            { required: true, message: '请输入确认密码!' },
            { min: 8, message: '确认密码长度不能小于8位' },
          ]"
      >
        <a-input-password v-model:value="userForm.checkPassword" placeholder="请输入密码" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">{{ route.query?.id ? '修改' : '创建' }}</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import {} from '@/api/pictureController.ts'
import { useRoute, useRouter } from 'vue-router'
import { getUserVoByIdUsingGet, updateSelfUsingPost } from '@/api/authUserController.ts'
import PictureUpload from '@/components/PictureUpload.vue'

const authUser = ref<API.AuthUserVO>()
const userForm = reactive<API.AuthUserUpdateSelfDTO>({})


const router = useRouter()

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  const userId = authUser.value.id
  if (!userId) {
    return
  }
  values.avatar = userForm.avatar
  try {
    const res = await updateSelfUsingPost({
      id: userId,
      ...values
    })
    //操作成功
    if (res.data.code === 0 && res.data.data) {
      message.success('创建成功')
      router.push(`/`)
    } else {
      message.error('创建失败,' + res.data.message)
    }
  } catch (e: any) {
    message.error('创建失败,' + e.message)
  }
}

const route = useRoute()

//获取老数据
const getOlderPicture = async () => {
  //获取到id
  const id = route.query?.id
  if (id) {
    const res = await getUserVoByIdUsingGet({
      id
    })
    if (res.data.code === 0 && res.data.data) {
      const data = res.data.data
      authUser.value = data
      userForm.userName = data.userName
      userForm.nickName = data.nickName
      userForm.phone = data.phone
      userForm.sex = data.sex
      userForm.avatar = data.avatar
    }
  }
}

const onSuccess = (newPicture: API.PictureVO) => {
  userForm.avatar = newPicture.url
}

onMounted(() => {
  getOlderPicture()
})
</script>

<style scoped>
#addPicturePage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
